package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.BaseMaintenanceMapper;
import com.kaili.basic.pojo.dto.BaseMaintenanceAddDto;
import com.kaili.basic.pojo.dto.BaseMaintenanceUpdateDto;
import com.kaili.basic.pojo.entity.BaseMaintenance;
import com.kaili.basic.service.BaseMaintenanceService;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by renzhikun on 18-6-28.
 */
@Service
public class BaseMaintenanceServiceImpl  implements BaseMaintenanceService{

    @Autowired
    private BaseMaintenanceMapper maintenanceMapper;

    @Override
    public void addBaseMaintenance(BaseMaintenanceAddDto baseMaintenanceAddDto) {
        Integer uid = LoginDetailUtils.getCurrentEmployeeId();
        BaseMaintenance baseMaintenance = new BaseMaintenance();
        BeanUtils.copyProperties(baseMaintenanceAddDto, baseMaintenance);
        if (uid != null) {
            baseMaintenance.setCreateBy(uid.longValue());
            baseMaintenance.setModifyBy(uid.longValue());
            maintenanceMapper.addBaseMaintenance(baseMaintenance);
        }
    }

    @Override
    public void deleteBaseMaintenance(Long maintenanceId) {
        Map<String, Object> map = new HashMap();
        Integer uid = LoginDetailUtils.getCurrentEmployeeId();
        if (uid != null) {
            map.put("modifyBy", uid.longValue());
            map.put("maintenanceId",maintenanceId);
            maintenanceMapper.deleteBaseMaintenance(map);
        }
    }

    @Override
    public void updateBaseMaintenance(BaseMaintenanceUpdateDto baseMaintenanceupDto) {
        Integer uid = LoginDetailUtils.getCurrentEmployeeId();
        BaseMaintenance baseMaintenance = new BaseMaintenance();
        BeanUtils.copyProperties(baseMaintenanceupDto, baseMaintenance);
        if (uid != null) {
            baseMaintenance.setModifyBy(uid.longValue());
            maintenanceMapper.updateBaseMaintenance(baseMaintenance);
        }
    }

    @Override
    public Page<BaseMaintenance> queryBaseMaintenance(Page<BaseMaintenance> page, BaseMaintenance maintenance) {
        List<BaseMaintenance> list = maintenanceMapper.queryBaseMaintenance(page, maintenance);
        return page.setRecords(list);
    }
}
